package com.dibang.app.operation.view.impl;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dibang.app.R;
import com.dibang.app.bean.GroupSelectNumberItem;
import com.dibang.app.mvp.abs.AbsView;
import com.dibang.app.mvp.util.DPUtil;
import com.dibang.app.operation.control.abs.IssueGroupControl;
import com.dibang.app.operation.model.abs.IssueGroupModel;
import com.dibang.app.operation.presenter.abs.IssueGroupPresenter;
import com.dibang.app.operation.view.abs.IssueGroupView;
import com.dibang.app.view.RxPopWindow;
import com.dibang.app.view.ViewHelper;
import com.dx.dxloadingbutton.lib.LoadingButton;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by wxl on 2019/7/24.
 *
 */

public class IssueGroupViewImpl extends AbsView<IssueGroupControl, IssueGroupView, IssueGroupPresenter, IssueGroupModel> implements IssueGroupView {

    private RxPopWindow.PopupWindowBuilder builder, remarkBuilder;

    private RxPopWindow popWindow, remarkPopWindow;

    private BaseQuickAdapter adapter = null,remarkAdapter;

    private List<GroupSelectNumberItem> remarks;

    private HashMap<Integer,String> remarkSelectedMap = new HashMap<>();

    @Override
    public void onShowGroupTip(int type, int num, @NonNull String tipText, @NonNull View targetView) {
        View popView = LayoutInflater.from(targetView.getContext()).inflate(R.layout.group_num_select_tip_layout, null);
        TextView tipView = popView.findViewById(R.id.group_num_select_tip_text_view);
        RecyclerView numView = popView.findViewById(R.id.group_num_select_tip_list_view);
        tipView.setText(tipText);
        if (type != 3) {
            onBindNumListViewChanged(type, num, numView);
            showNumberPop(popView, targetView);
        } else {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) numView.getLayoutParams();
            params.bottomMargin = 0;
            onBindRemarkListViewChanged(numView);
            onShowRemarkPop(popView, targetView);
        }
    }


    /**
     * 备注
     *
     * @param remarkView
     */
    private void onBindRemarkListViewChanged(RecyclerView remarkView) {
        ViewHelper.setRecyclerViewGridLayoutManager(remarkView, 2);
        if (remarks == null) {
            remarks = presenter.getRemarks(0);
        }
        remarkView.setAdapter(remarkAdapter = new BaseQuickAdapter<GroupSelectNumberItem, BaseViewHolder>(R.layout.itme_group_tip_num_select_layout, remarks) {
            @Override
            protected void convert(BaseViewHolder helper, GroupSelectNumberItem item) {
                ViewGroup.LayoutParams layoutParams = helper.itemView.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                TextView textView = helper.getView(R.id.item_group_select_num_pop_text_view);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) textView.getLayoutParams();
                params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                params.height = DPUtil.dip2px(textView.getContext(),35);
                params.bottomMargin = DPUtil.dip2px(textView.getContext(),5);
                helper.setText(R.id.item_group_select_num_pop_text_view, item.remark);
                helper.setBackgroundRes(R.id.item_group_select_num_pop_text_view, item.select ? R.drawable.shape_blue_radius_5_bg : R.drawable.shape_brold_gray_radius_5_bg);
                helper.setVisible(R.id.item_group_select_num_pop_check,item.select);
                helper.setOnClickListener(R.id.item_group_select_num_pop_text_view, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        notifyRemarkItemClickChanged(helper.getAdapterPosition());
                    }
                });
            }
        });
    }


    /**
     * 点击刷新状态
     * @param position
     */
    private void notifyRemarkItemClickChanged(int position){
        remarks.get(position).select = !remarks.get(position).select;
        remarkAdapter.notifyDataSetChanged();
        if(remarkSelectedMap.containsKey(position) && !remarks.get(position).select){
            remarkSelectedMap.remove(position);
        } else {
            remarkSelectedMap.put(position,remarks.get(position).remark);
        }
    }


    /**
     * 显示备注信息
     *
     * @param popView
     * @param targetView
     */
    private void onShowRemarkPop(@NonNull View popView, @NonNull View targetView) {
        LoadingButton button = popView.findViewById(R.id.group_num_select_tip_submit_view);
        ViewHelper.setLoadingButtonBackgroundShader(button,targetView.getContext());
        button.setVisibility(View.VISIBLE);
        if (remarkBuilder == null) {
            remarkBuilder = ViewHelper.generatePopupWindowBuilder(250, targetView.getContext());
            remarkPopWindow = remarkBuilder.setView(popView)
                    .create()
                    .showAtLocation(targetView, Gravity.BOTTOM, 0, 0);
        } else {
            remarkPopWindow = remarkBuilder.setView(popView)
                    .create()
                    .showAtLocation(targetView, Gravity.BOTTOM, 0, 0);
        }

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remarkPopWindow.dissmiss();
                if(!remarkSelectedMap.isEmpty()){
                    onRemarkSelectedGroupRes();
                } else {
                    control.onRemarkBuffer("");
                }
            }
        });
    }


    /**
     * 组合选择后的字符串回调到C层
     */
    private void onRemarkSelectedGroupRes(){
        Iterator<Map.Entry<Integer, String>> iterator = remarkSelectedMap.entrySet().iterator();
        StringBuffer buffer = new StringBuffer();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> next = iterator.next();
            buffer.append(next.getValue()+";");
        }
        StringBuffer delete = buffer.delete(buffer.length() - 1, buffer.length());
        control.onRemarkBuffer(delete.toString());
    }


    /**
     * 显示弹窗
     *
     * @param popView
     * @param targetView
     */
    private void showNumberPop(@NonNull View popView, @NonNull View targetView) {
        if (builder == null) {
            builder = ViewHelper.generatePopupWindowBuilder(250, targetView.getContext());
            popWindow = builder.setView(popView)
                    .create()
                    .showAtLocation(targetView, Gravity.BOTTOM, 0, 0);
        } else {
            popWindow = builder.setView(popView)
                    .create()
                    .showAtLocation(targetView, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 设置数据
     *
     * @param numView
     */
    private void onBindNumListViewChanged(int type, int num, RecyclerView numView) {
        ViewHelper.setRecyclerViewSingleLineManager(numView);
        List<GroupSelectNumberItem> numbers = presenter.getNumbers(num, 1);
        numView.setAdapter(adapter = new BaseQuickAdapter<GroupSelectNumberItem, BaseViewHolder>(R.layout.itme_group_tip_num_select_layout, numbers) {
            @Override
            protected void convert(BaseViewHolder helper, GroupSelectNumberItem item) {
                helper.setText(R.id.item_group_select_num_pop_text_view, item.num + "人");
                helper.setBackgroundRes(R.id.item_group_select_num_pop_text_view, item.select ? R.drawable.shape_blue_radius_5_bg : R.drawable.shape_brold_gray_radius_5_bg);
                helper.setOnClickListener(R.id.item_group_select_num_pop_text_view, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onNumberItemClick(type, adapter, helper.getAdapterPosition(), numbers);
                        if (popWindow != null) {
                            popWindow.dissmiss();
                        }
                    }
                });
            }
        });
    }


    /**
     * 点击选择人数
     *
     * @param type
     * @param adapter
     * @param position
     * @param numbers
     */
    private void onNumberItemClick(int type, BaseQuickAdapter adapter, int position, List<GroupSelectNumberItem> numbers) {
        for (int i = 0; i < numbers.size(); i++) {
            numbers.get(i).select = i == position;
        }
        adapter.setNewData(numbers);
        control.onSelectNumber(type, numbers.get(position).num);
    }

}
